#include "ResultPaintDialog.h"

ResultPaintDialog::ResultPaintDialog(QWidget *parent) :
    QDialog(parent)
{
    schedulePixmap = new QPixmap;
    legendPixmap = new QPixmap;

    scheduleLable = new QLabel;
    legendLabel = new QLabel;

    closeButton = new QPushButton(tr("Close"));
    closeButton->setFixedWidth( 150 );

    mainLayout = new QVBoxLayout;
    mainLayout->addWidget( scheduleLable );
    mainLayout->addSpacing( indentSize );
    mainLayout->addWidget( legendLabel );
    mainLayout->addWidget( closeButton, Qt::AlignRight );

    //setWindowState( Qt::WindowMaximized );
    setWindowTitle( tr("Results") );

    makeResize();

    setLayout( mainLayout );

    connect( closeButton, SIGNAL(clicked()), this, SLOT(close()) );
}

void ResultPaintDialog::makeResize()
{
    QRect windowRect;
    windowRect.setRect(0,0,800,800);
    int printArea = windowRect.height() - 3*indentSize;

    scheduleRect.setRect( 0, 0,
                          windowRect.width()-2*indentSize,
                          (int)( (double)printArea*0.8) );

    legendRect.setRect( 0, 0,
                        windowRect.width()-2*indentSize,
                        (int)( (double)printArea*0.2) );

    *schedulePixmap = QPixmap( scheduleRect.width(), scheduleRect.height() );
    *legendPixmap = QPixmap( legendRect.width(), legendRect.height() );

    scheduleLable->setPixmap( *schedulePixmap );
    legendLabel->setPixmap( *legendPixmap );
}

/*void ResultPaintDialog::changeEvent(QEvent *event)
{
    if (event->type() == QEvent::WindowStateChange)
        makeResize();
}*/

void ResultPaintDialog::drawSchedule()
{
    QPainter p;
    p.begin( schedulePixmap );

    int n = input.size();
    int m = input[0].size();

    int columnWidth = (scheduleRect.width() - 2 * indentSize) / n;
    if (columnWidth > maxColumnWidth) columnWidth = maxColumnWidth;

    int mx = -1;
    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
            if ( output[i][j] + input[i][j] > mx ) mx = output[i][j] + input[i][j];

    double coef = (double)(scheduleRect.height() - 2*indentSize) / (double)mx;

    //generating colors for every detail
    colors.resize( m );

    for (int i=0; i<m; i++)
        colors[i] = new QColor(150 + qrand()%100, 150 + qrand()%100, 150 + qrand()%100);

    //drawing
    int x = scheduleRect.center().x() - n*columnWidth/2;

    p.fillRect( schedulePixmap->rect(), QBrush(Qt::white) );

    //draw axes
    p.setPen( QPen( QBrush(Qt::black), 2 ) );
    //drawLine(100,100, 200,200);
    p.drawLine( indentSize, scheduleRect.bottom() - indentSize,
                indentSize, indentSize);
    p.drawLine( indentSize, scheduleRect.bottom() - indentSize,
                scheduleRect.right() - indentSize, scheduleRect.bottom() - indentSize);
    //p.setPen( QPen( QBrush(Qt::black), 2 ) );

    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++){
           p.setBrush( QBrush( *colors[j] ) );
           p.setPen( QPen( QBrush(Qt::black), 2 ) );
           p.drawRect( x + i*columnWidth,
                       scheduleRect.bottom() - indentSize - (int)(coef*(double)(output[i][j] + input[i][j])),
                       columnWidth,
                       coef*input[i][j]);

           //draw end time
           p.setFont( QFont("Arial", 12, 50, true ) );
           p.setPen( QPen( QBrush(Qt::black), 2 ) );
           p.drawText( QRect(x + i*columnWidth,
                       scheduleRect.bottom() - indentSize - (int)(coef*(double)(output[i][j] + input[i][j])),
                       columnWidth, 20),
                       QString::number( input[i][j] + output[i][j] ),
                       QTextOption(Qt::AlignVCenter | Qt::AlignHCenter) );

           //draw red line
//           p.setPen( QPen( QBrush(Qt::red), 2 ) );
//           p.drawLine( indentSize, indentSize-2, scheduleRect.right() - indentSize, indentSize-2 );

           //draw machine headers
           p.setFont( QFont("Arial", 20 ) );
           p.setPen( QPen( QBrush(Qt::black), 2 ) );
           p.drawText( QRect(x + i*columnWidth, scheduleRect.bottom() - indentSize,
                             columnWidth, indentSize), QString::number( i ),
                       QTextOption( Qt::AlignHCenter | Qt::AlignVCenter ) );
       }

    p.end();

    scheduleLable->setPixmap( *schedulePixmap );
}

void ResultPaintDialog::drawLegend()
{
    QPainter p;
    p.begin( legendPixmap );

    p.fillRect( schedulePixmap->rect(), QBrush(Qt::white) );

    int sectionWidth = 70;
    int smallHeight = (legendRect.height() / 3) / 5;

    int m = input[0].size();

    for (int i=0; i<m; i++){
        //draw example rect
        p.setBrush( QBrush( *colors[i] ) );
        p.setPen( QPen( QBrush(Qt::black), 2 ) );

        p.drawRect( i/3*sectionWidth + smallHeight,
                    smallHeight + i%3 * (5*smallHeight),
                    0.3*sectionWidth,
                    3*smallHeight);

        p.setFont( QFont("Arial", 20 ) );
        p.drawText( QRect( i/3*sectionWidth + smallHeight + 0.3*sectionWidth,
                           smallHeight + i%3 * (5*smallHeight),
                           sectionWidth - 3*smallHeight,
                           3*smallHeight),
                    QString(tr("- %1").arg(i)),
                    QTextOption( Qt::AlignVCenter | Qt::AlignHCenter ));
    }

    p.end();

    legendLabel->setPixmap( *legendPixmap );
}

void ResultPaintDialog::setTaskParams(QVector<QVector<int> >input, QVector<QVector<int> >output)
{
    this->input = input;
    this->output = output;

    drawSchedule();
    drawLegend();
}
